package com.ruoyi.teacher.teacherUtils;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.taobao.api.ApiException;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

public class RosterUtil {

    // 应用的appKey
    private static String appKey = "dingod4uaw4goav3zmav";
    // 应用的appSecret
    private static String appSecret = "5Eax_ynl070y5dpwqfAgJ0Ulnsw-GwGOazhOWE2t47UqZXTcBO1ek-oWy2mE4tu-";
    // 创建人id
    private static String opUserId = "manager2215";
    // 考勤组id
    private static Long groupId = 1040530117L;
    // 班次id
    private static Long shiftId = 1040515157L;

    /**
     * 通过调用接口获取access_token
     * dingtalk
     * @return
     * String
     * 2021-1-20
     */
    public static String getAccessToken() {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey(appKey);
        request.setAppsecret(appSecret);
        request.setHttpMethod("GET");
        OapiGettokenResponse response = null;
        try {
            response = client.execute(request);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return response.getAccessToken();
    }

    /**
     * 批量查询成员排班概要信息
     * 调用本接口，查询用户在某个时间段内的排班概要信息。
     * 调用本接口，获取成员排班概要信息，包括班次名称、班次版本ID、考勤组ID等内容
     * @param access_token
     * @throws ApiException
     */
    public static void getRosterSummary(Long timeStamp, String access_token){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/schedule/shift/listbydays");
        OapiAttendanceScheduleShiftListbydaysRequest req = new OapiAttendanceScheduleShiftListbydaysRequest();
        req.setOpUserId(opUserId);
        req.setUserids("manager123,015331436801841164");
        req.setFromDateTime(1671947413000L);
        req.setToDateTime(timeStamp);
        OapiAttendanceScheduleShiftListbydaysResponse rsp = null;
        try {
            rsp = client.execute(req, access_token);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        System.out.println(rsp.getBody());
    }

    /**
     * 批量查询人员排班信息
     * 调用本接口，批量查询员工在某天的排班信息。
     * 例如，员工小钉和小明所在的考勤组信息如下图所示，考勤组内设置的班次为默认班次，上班时间为09：30，下班时间为18：30。
     * 调用本接口，可获取小钉和小明2022年6月14日的排班信息，小钉和小明在2022年6月14日的班次信息和上下班时间等。
     */
    public static String getRosterInfo() {
        String accessToken = getAccessToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/schedule/listbyusers");
        OapiAttendanceScheduleListbyusersRequest req = new OapiAttendanceScheduleListbyusersRequest();
        req.setOpUserId(opUserId);
        req.setUserids("manager2215,015331436801841164");
        OapiAttendanceScheduleListbyusersResponse rsp = null;
        try {
            List<Long> timeStamp = TimeUtil.getTimeStamp();
            req.setFromDateTime(timeStamp.get(0));
            req.setToDateTime(timeStamp.get(1));
            rsp = client.execute(req, accessToken);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rsp.getBody();
    }

    /**
     * 组内人员排班
     */
    public static String arrangeRoster(Long groupId, Long shiftId, Long workDate, String userId) {
        String accessToken = getAccessToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/group/schedule/async");
        OapiAttendanceGroupScheduleAsyncRequest req = new OapiAttendanceGroupScheduleAsyncRequest();
        req.setOpUserId("manager2215");
        req.setGroupId(groupId);
        List<OapiAttendanceGroupScheduleAsyncRequest.TopScheduleParam> list2 = new ArrayList<>();
        OapiAttendanceGroupScheduleAsyncRequest.TopScheduleParam obj3 = new OapiAttendanceGroupScheduleAsyncRequest.TopScheduleParam();
        list2.add(obj3);
        // 当setShiftId = 1，setIsRest = true时为休息
        obj3.setShiftId(shiftId);
        obj3.setIsRest(false);
        obj3.setWorkDate(workDate);
        obj3.setUserid(userId);
        req.setSchedules(list2);
        OapiAttendanceGroupScheduleAsyncResponse rsp = null;
        try {
            rsp = client.execute(req, accessToken);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        System.out.println(rsp.getBody());
        return rsp.getBody();
    }

    public static void main(String[] args) throws ApiException {
//        arrangeRoster();
//        getGroupInfo();
//        getShiftInfo();
//        getRosterInfo();
//        getGroupInfo();
    }

    /**
     * 获取用户所在的考勤组和班次
     * @throws ApiException
     */
    public static String getUserGroup(String userid) {
        String accessToken = getAccessToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getusergroup");
        OapiAttendanceGetusergroupRequest req = new OapiAttendanceGetusergroupRequest();
        req.setUserid(userid);
        OapiAttendanceGetusergroupResponse rsp = null;
        try {
            rsp = client.execute(req, accessToken);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return rsp.getBody();
    }

    /**
     * 批量查询成员排班概要信息
     * @throws ApiException
     */
    private static void getRosterOutlineInfo() {
        String accessToken = getAccessToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/schedule/shift/listbydays");
        OapiAttendanceScheduleShiftListbydaysRequest req = new OapiAttendanceScheduleShiftListbydaysRequest();
        req.setOpUserId("manager2215");
        req.setUserids("manager2215,183105046735138687");
        List<Long> timeStamp = null;
        try {
            timeStamp = TimeUtil.getTimeStamp();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        req.setFromDateTime(timeStamp.get(0));
        req.setToDateTime(timeStamp.get(1));
        OapiAttendanceScheduleShiftListbydaysResponse rsp = null;
        try {
            rsp = client.execute(req, accessToken);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        System.out.println(rsp.getBody());
    }

    /**
     * 获取班次信息
     * @throws ApiException
     */
    private static void getShiftInfo() {
        String accessToken = getAccessToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/shift/list");
        OapiAttendanceShiftListRequest req = new OapiAttendanceShiftListRequest();
        req.setOpUserId("dd_test");
        req.setCursor(0L);
        OapiAttendanceShiftListResponse rsp = null;
        try {
            rsp = client.execute(req, accessToken);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        System.out.println(rsp.getBody());
    }

    /**
     * 获取考勤组信息
     */
    private static void getGroupInfo() {
        String accessToken = getAccessToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/group/minimalism/list");
        OapiAttendanceGroupMinimalismListRequest req = new OapiAttendanceGroupMinimalismListRequest();
        req.setOpUserId("manager2215");
        req.setCursor(0L);
        OapiAttendanceGroupMinimalismListResponse rsp = null;
        try {
            rsp = client.execute(req, accessToken);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        System.out.println(rsp.getBody());
    }

}
